<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;

use fast\Tree;

/**
 * 会员中心
 */
class Team extends Frontend
{

    protected $layout = 'default';
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 会员中心
     */
    public function index($id=0)
    {

        /*$userList = model('User')->where('status', 'normal')->order('node_parent_id', 'desc')->column('id,group_id,username,createtime,node_parent_id');
        foreach ($userList as $k => &$v)
        {
            $v['createtime'] = datetime($v['createtime'], 'Y-m-d');
        }
        unset($v);*/

        if ($id>0) {
            $topUser = db('user')->where('id', $id)->find();
        }else{
            $topUser = $this->auth->getUser();
            //dump($topUser);
            $id = $this->auth->id;
        }

        //echo $id;exit;

        $username = $this->request->request('username');
        if ($username) {
            $queryUser = db('user')->where('username', $username)->find();
            if ($queryUser) {
                $topUser = $queryUser;
                $id = $queryUser['id'];
            }
        }
        $this->view->assign('topUser', $topUser);

        // 等级配置信息
        $group_list = db('UserGroup')->column('id, name');
        $this->view->assign('group_list', $group_list);

        $firstChildren = db('user')->where('node_parent_id', $id)->order('id asc')->column('id');

        // 共享社区人数
        $teamNum = 0;
        if (isset($firstChildren[1])) {
            $teamNum = model('User')->getTeamNum($firstChildren[1]);
            $teamNum += 1;
        }
        $this->view->assign('teamNum', $teamNum);

        // 分享社区人数
        $childrenNum = 0;
        if (isset($firstChildren[0])) {
            $childrenNum = model('User')->getTeamNum($firstChildren[0]);
            $childrenNum += 1;
        }
        $this->view->assign('childrenNum', $childrenNum);


        $teamList = model('User')->getTeam($id);
        //dump($teamList);

        $this->view->assign('teamList', $teamList);

        $this->view->assign('title', __('User team'));
        
        return $this->view->fetch();
    }

}
